#!/usr/bin/env bash

# DESCRIPTION
#     This is the patch script to to prevent the game from using unsupported gpu features
#
# EXIT STATUS
#     0      : The patch was applied successfully.
#     other  : Patch failure. See stdout or stderr output for details.


# Templates for the next patch testing phase
#echo " === !! UNTESTED PATCH. CHECK FOR BANS USING A TRASH ACCOUNT !! ==="
#echo " ===      REPORT RESULTS TO THE MAIN REPOSITORY. THANK YOU.     ==="
#echo ""
#echo "If you would like to test this patch, modify this script and remove the line below this one."
#exit 1

if ! [[ $(uname) == "Darwin" ]]; then
	echo "You don't need this patch if you are not on macOS."
    exit 1
fi

DIR=$(dirname "${BASH_SOURCE[0]}")
DATADIR=$(find . -maxdepth 1 -type d -name "*_Data")
GGM="$DATADIR/globalgamemanagers"

if [[ -e "$GGM.bak" ]]; then
    echo "A backup already exists"
    exit 1
fi

echo "--- Patching globalgamemanagers"

mv -f "$GGM" "$GGM.bak"
# this works for both CN/OS server
xxd -p -c 100000000 < "$GGM.bak" | sed "s/67616d657300000001/67616d657300000000/" | xxd -r -p > "$GGM"

# Done!
echo "==> Patch applied! Enjoy the game."
echo
echo "[NOTICE]  Please refrain from sharing this project in public so"
echo "          that there can be Linux patches in the future. Thank you."

exit 0
